﻿using LayuiMvc.DataModel.Community;
using LayuiMvc.Logic.Community;
using LayuiMvc.Logic.LogicEnum;
using LayuiMvc.Util.EnumUtil;
using LayuiMvcUtil.Exception;
using LayuiMvcUtil.Result;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace LayuiMvc.WebUI.Areas.Admin.Controllers
{
    public class PostManageController : Controller
    {
        /// <summary>
        /// 帖子管理业务逻辑类
        /// </summary>
        PostManageService postManageService = new PostManageService();

        PostService postService = new PostService();
        /// <summary>
        /// 帖子管理视图跳转
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 帖子管理列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="postStatus"></param>
        /// <param name="postType"></param>
        /// <param name="postSort"></param>
        /// <returns></returns>
        public async Task<ActionResult> PostManageList(int page,int limit,
            PostStatusQueryEnum[] postStatus,
            PostTypeQueryEnum postType=PostTypeQueryEnum.None,
            PostSortQueryEnum postSort = PostSortQueryEnum.New)
        {
            DataResult<List<PostManageModel>> rtnResult = await postManageService.PostManageListAsync(page, limit, postType, postStatus, postSort);

            return Json(rtnResult, JsonRequestBehavior.AllowGet);
        }

        #region 分类列表

        /// <summary>
        /// 帖子分类列表
        /// </summary>
        /// <returns></returns>
        public ActionResult PostTypeList()
        {
            DataResult<List<ComboboxResult>> rtnResult = new DataResult<List<ComboboxResult>>();

            List<ComboboxResult> postTypeList = EnumExtend.GetEnumDefineAttributes<PostTypeQueryEnum>("PostTypeQuery")
                .Select(m => new ComboboxResult()
                {
                    Value = m.Value,
                    Text = m.Description
                }).ToList();

            rtnResult.data = postTypeList;

            return Json(rtnResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 帖子状态列表
        /// </summary>
        /// <returns></returns>
        public ActionResult PostStatusList()
        {
            DataResult<List<ComboboxResult>> rtnResult = new DataResult<List<ComboboxResult>>();

            List<ComboboxResult> postStatusList = EnumExtend.GetEnumDefineAttributes<PostStatusQueryEnum>("PostStatusQuery")
                .Select(m => new ComboboxResult()
                {
                    Value = m.Value,
                    Text = m.Description
                }).ToList();

            rtnResult.data = postStatusList;

            return Json(rtnResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 帖子排序列表
        /// </summary>
        /// <returns></returns>
        public ActionResult PostSortList()
        {
            DataResult<List<ComboboxResult>> rtnResult = new DataResult<List<ComboboxResult>>();

            List<ComboboxResult> postSortList = EnumExtend.GetEnumDefineAttributes<PostSortQueryEnum>("PostSortQuery")
                .Select(m => new ComboboxResult()
                {
                    Value = m.Value,
                    Text = m.Description
                }).ToList();

            rtnResult.data = postSortList;

            return Json(rtnResult, JsonRequestBehavior.AllowGet);
        }

        #endregion

        #region 帖子操作

        /// <summary>
        /// 帖子操作
        /// </summary>
        /// <param name="postId">帖子ID</param>
        /// <param name="opera">帖子操作</param>
        /// <returns></returns>
        public async Task<ActionResult> PostOperate(int postId, PostOperateEnum opera)
        {
            DataResult<string> rtnResult = new DataResult<string>();

            try
            {
                int userId = HttpContext.User.Identity.GetUserId<int>();

                await postService.PostOperateAsync(postId, userId, opera);
            }
            catch (CustomException exception)
            {
                rtnResult.code = 1;
                rtnResult.msg = exception.Message;
            }

            return Json(rtnResult);
        }


        #endregion
    }
}